const Router = require('koa-router');
const AcademyRouter = new Router();
const AcademyService = require('../service/AcademyService');
const R = require('../utils/R');
const Validators = require('../utils/validators');

AcademyRouter.prefix('/api/v1/academy');

AcademyRouter.get('/list',async (ctx, next) => {
    let data = await AcademyService.findAll();
    if(data.name){
        ctx.body = R.errorMsg(data.name)
    }else {
        ctx.body = R.okData(data)
    }
})

// AcademyRouter.get('/info/:id', async (ctx, next) => {
//     let academyId = ctx.params.id;
//     let data = await AcademyService.findById(academyId);

//     if(data.name){
//         ctx.body = R.errorMsg(data.name)
//     }else {
//         ctx.body = R.okData(data)
//     }
// })

AcademyRouter.post('/save', async (ctx, next) => {
    let academyObj = ctx.request.body;
    Validators.isNull(academyObj.academyName, 'academyName')
    await AcademyService.saveAcademy(academyObj);

    ctx.body = R.okMsg('添加成功');
})

AcademyRouter.post('/delete', async (ctx, next) => {
    let academyObj = ctx.request.body;
    Validators.isNull(academyObj.academyId, 'academyId')
    await AcademyService.deleteAcademy(academyObj.academyId);

    ctx.body = R.okMsg('删除成功')
})

// AcademyRouter.post('/update', async (ctx, next) => {
//     let academyObj = ctx.request.body;
//     let data = await AcademyService.updateAcademy(academyObj);
//     // console.log('data--->', data[0]);
//     if(data.name || data[0] == 0){
//         ctx.body = R.errorMsg(data.name || '修改失败')
//     }else {
//         ctx.body = R.okMsg('修改成功')
//     }
// })

module.exports = AcademyRouter